package com.xhs.manager.impl;


import com.xhs.config.SystemConfig;
import com.xhs.entity.OssLog;
import com.xhs.manager.intf.AliOssService;
import com.xhs.service.intf.OssLogService;
import com.xhs.utils.*;
import com.xhs.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

@Service
public class AliOssServiceImpl implements AliOssService {
    @Autowired
    private OssLogService service;

    @Override
    public R upload(String fn, byte[] data) {
        //1.重命名并验证内容
        fn= FileUtils.rename(fn);
        if(data.length>0){
            //2.上传到阿里云的OSS
            Date cdate= DateUtil.getDateYear(SystemConfig.OSS_URL_TIME);
            String url= AliOssUtils.upload(fn, data,cdate);
            if(StringUtils.isNotEmpty(url)){
                //3.新增数据库
                OssLog log=new OssLog(SystemConfig.OSS_TYPE_HEAD,AliOssUtils.bucketName,fn,url,cdate,new Date());
                //4.返回
                return RUtils.ok(url);
            }
        }
        return RUtils.fail("网络异常，上传失败！");
    }
}
